package com.rc.saas.mini.service.elec;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.mini.mapper.discount.TenantDiscountPolicyMapper;
import com.rc.saas.mini.mapper.elec.ElecStationMapper;
import com.rc.saas.mini.mapper.member.MemberTicketMapper;
import com.rc.saas.mini.mapper.tenant.TenantTicketStationMapper;
import com.rc.saas.mini.model.discount.TenantDiscountPolicy;
import com.rc.saas.mini.model.discount.TenantDiscountPolicyExample;
import com.rc.saas.mini.model.elec.ElecStation;
import com.rc.saas.mini.model.tenant.TenantTicketStation;
import com.rc.saas.mini.model.tenant.TenantTicketStationExample;
import com.rc.saas.mini.search.ElecStationSearch;
import com.rc.saas.mini.vo.elec.ElecStationUsableVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wangzhengwei
 * @create 2021/12/21
 */
@Service
public class ElecStationServiceImpl implements ElecStationService {

    @Autowired
    private ElecStationMapper elecStationMapper;

    @Autowired
    private TenantDiscountPolicyMapper tenantDiscountPolicyMapper;

    @Autowired
    private MemberTicketMapper memberTicketMapper;

    @Autowired
    private TenantTicketStationMapper tenantTicketStationMapper;

    @Override
    public ElecStationMapper getMapper() {
        return elecStationMapper;
    }

    @Override
    public List<Long> listActiveStation(String tenantCode) {
        TenantDiscountPolicyExample discountPolicyExample = new TenantDiscountPolicyExample();
        discountPolicyExample.createCriteria().andTenantCodeEqualTo(tenantCode)
                .andBeginTimeLessThanOrEqualTo(new Date())
                .andEndTimeGreaterThanOrEqualTo(new Date())
                .andStatusEqualTo("1");
        List<TenantDiscountPolicy> list = tenantDiscountPolicyMapper.selectByExample(discountPolicyExample);
        List<Long> idList = list.stream().map(TenantDiscountPolicy::getElecStationId).collect(Collectors.toList());
        return idList;
    }

    @Override
    public List<Long> listAvailableCouponStation(String tenantCode, Long memberId) {
        List<Long> ticketTypes = memberTicketMapper.listTicketType(memberId);

        TenantTicketStationExample tenantTicketStationExample = new TenantTicketStationExample();
        tenantTicketStationExample.createCriteria().andTenantCodeEqualTo(tenantCode).andTicketTypeIdIn(ticketTypes);
        return tenantTicketStationMapper.selectByExample(tenantTicketStationExample)
                .stream().map(TenantTicketStation::getElecStationId).distinct().collect(Collectors.toList());
    }

    @Override
    public List<ElecStation> listElecStation(ElecStationSearch search) {
        List<ElecStation> list = elecStationMapper.listStationBySearch(search);
        return list;
    }

    @Override
    public PageInfo<ElecStation> listPageElecStation(ElecStationSearch search, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<ElecStation> list = elecStationMapper.listStationBySearch(search);
        return new PageInfo<>(list);
    }

    @Override
    public List<ElecStationUsableVo> listElecStationInfo(List<Long> elecStationIds) {
        return elecStationMapper.listElecStationInfo(elecStationIds);
    }

    @Override
    public ElecStationUsableVo countElecStationInfoById(Long elecStationId) {
        return elecStationMapper.countElecStationInfoById(elecStationId);
    }

    @Override
    public PageInfo<ElecStation> listPageCollectByMember(ElecStationSearch search, int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<ElecStation> elecStationList = elecStationMapper.listCollection(search);
        return new PageInfo<>(elecStationList);
    }

    @Override
    public void updateFlag() {
        elecStationMapper.updateFlag();
    }

    @Override
    public ElecStation selectStationById(Long elecStationId, BigDecimal longitude, BigDecimal latitude) {
        return elecStationMapper.selectStationById(elecStationId, longitude, latitude);

    }
}
